package com.tszs.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tszs.biz.DistributorBiz;
import com.tszs.biz.HomeRotaryMapBiz;
import com.tszs.entity.Distributor;
import com.tszs.entity.DistributorExample;
import com.tszs.entity.HomeRotaryMap;
import com.tszs.entity.HomeRotaryMapExample;
import com.tszs.util.CallBack;
import com.tszs.util.Uuid;
import com.tszs.util.projectPath;

import net.sf.json.JSONObject;
/**
 * @类名:AdministratorTableController
 * @类描述：管理员表操作 @创建时间：2019-03-22 13:07:07
 * @创建人：王俊
 *
 */

@Controller
public class HomeRotaryMapController {
	/**
	 * @AdministratorTableBiz
	 * @创建时间:201903-21 13:14:14
	 */
	@Autowired
	private HomeRotaryMapBiz homeRotaryMapBiz;
	/**
	 * @方法名:HomeRotaryMapInsert
	 * @方法描述：前台图片轮播添加
	 * @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 *
	 */
	@RequestMapping("pc/admin/HomeRotaryMapInsert")
	public void HomeRotaryMapInsert(HttpServletRequest request,HttpServletResponse response) {
		HomeRotaryMapExample example=new HomeRotaryMapExample();
		List<HomeRotaryMap> selectByExample=homeRotaryMapBiz.selectByExample(example);
		String photo=request.getParameter("cPhoto");
		HomeRotaryMap record=new HomeRotaryMap();
		record.sethStatic("0");
		if(selectByExample.size()>0) {
			record.sethId(selectByExample.get(0).gethId());
			record.sethCreateTime(Uuid.getTime());
			record.sethPhoto(photo);
			record.setUserName(request.getSession().getAttribute("aUsername")+"");
			record.setUserId(request.getSession().getAttribute("aId")+"");
			int c=homeRotaryMapBiz.updateByExampleSelective(record, example);
			if(c>0) {
				new CallBack().yes(response, "","");
			}else {
				new CallBack().no(response, "","");
			}
		}else {
			record.sethId(Uuid.getUUID());
			record.sethCreateTime(Uuid.getTime());
			record.sethPhoto(photo);
			record.setUserName(request.getSession().getAttribute("aUsername")+"");
			record.setUserId(request.getSession().getAttribute("aId")+"");
			int insertSelective=homeRotaryMapBiz.insertSelective(record);
			if(insertSelective>0) {
				new CallBack().yes(response, "","");
			}else {
				new CallBack().no(response, "","");
			}
		}
		
	}
	/**
	 * @方法名:HomeRotaryMapPage
	 * @方法描述：前台图片轮播
	 * @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 *
	 */
	@RequestMapping("pc/admin/HomeRotaryMapPage")
	public void findCommodityListPage(HttpServletRequest request,HttpServletResponse response) {
		response.setContentType("text/xml; charset=UTF-8");
		response.setContentType("text/html;charset=utf-8");
		int page = Integer.parseInt(request.getParameter("page"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		HomeRotaryMap example = new HomeRotaryMap();
		example.setPage((page - 1) * limit);
		example.setLimit(limit);
		//TODO
		List<HomeRotaryMap> selectByExample = homeRotaryMapBiz.selectByExamplePage(example);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", selectByExample.size());
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < selectByExample.size(); i++) {
			Map<String, Object> mapData = new HashMap<String, Object>();
			String b="";
			for (int c = 0; c < selectByExample.get(i).gethPhoto().split(",").length; c++) {
				b+="<a target=\"_Blank\" href='"+projectPath.url+selectByExample.get(i).gethPhoto().split(",")[c]+"'><img src='"+projectPath.url+selectByExample.get(i).gethPhoto().split(",")[c]+"'/></a>";
			}
			mapData.put("hId",i+1);
			mapData.put("hCreateTime", selectByExample.get(i).gethCreateTime());
			mapData.put("UserId", selectByExample.get(i).getUserId());
			mapData.put("UserName", selectByExample.get(i).getUserName());
			mapData.put("hPhoto",b);
			list.add(mapData);
		}
		map.put("data", list);
		JSONObject json = JSONObject.fromObject(map);
		new CallBack().textObject(json, response);
	}
	/**
	 * @方法名:HomeRotaryMapPage
	 * @方法描述：前台图片轮播
	 * @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 *
	 */
	@RequestMapping("txr/selectHomeRotary")
	public void selectHomeRotary(HttpServletRequest request,HttpServletResponse response) {
		HomeRotaryMapExample example=new HomeRotaryMapExample();
		List<HomeRotaryMap> selectByExample=	homeRotaryMapBiz.selectByExample(example);
		String b=projectPath.url;
		List<String> list=new ArrayList<>();
		for (int i = 0; i < selectByExample.size(); i++) {
		for (int c = 0; c < selectByExample.get(i).gethPhoto().split(",").length; c++) {
			b=selectByExample.get(i).gethPhoto().split(",")[c];
			list.add(b);
		}
		}
		new CallBack().yes(response, list, "");
	}
}
